﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;

namespace CodeCube.Metronic
{
    public static class Template
    {
        private static List<string> _CssFileList;
        private static List<string> _JsFileList;

        public static void Init()
        {
            using (Stream resources = Assembly.GetExecutingAssembly().GetManifestResourceStream("CodeCube.Metronic.MetronicV61.resources"))

            {

                //使用IResourceReader

                ReadUsingResourceReader(resources);

                //重新定位Stream

                resources.Seek(0, SeekOrigin.Begin);

                //使用ResourceSet

                //ReadUsingResourceSet(resources);

            }
        }//使用IResourceReader

        static void ReadUsingResourceReader(Stream st)

        {

            Console.WriteLine("== 使用IResourceReader");

            IResourceReader rr = new ResourceReader(st);

            var iter = rr.GetEnumerator();

            while (iter.MoveNext())

                Console.WriteLine("键: {0} 值: {1}", iter.Key, iter.Value);

            //不需要调用IResourceReader.Dispose，Stream会在Main方法中被Dipose

        }

        //使用ResourceSet

        static void ReadUsingResourceSet(Stream st)

        {

            Console.WriteLine("== 使用ResourceSet");

            ResourceSet rs = new ResourceSet(new ResourceReader(st));

            Console.WriteLine(BitConverter.ToString((byte[])rs.GetObject("CSS")));

            //Console.WriteLine(rs.GetString("String1"));

            //不需要调用ResourceSet.Dispose，Stream会在Main方法中被Dipose

        }
        /// <summary>
        /// 获取Css预加载数据
        /// </summary>
        /// <returns></returns>
        public async static Task<string> GetCssPreloading()
        {
            ResourceReader rr = new ResourceReader("MetronicV61");
            string rrs;
            byte[] rrb;
            rr.GetResourceData("Html", out rrs, out rrb);

            var tp = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />";
            var res = new StringBuilder();
            foreach(var s in _CssFileList)
            {
                res.Append(string.Format(tp, s));
            }
            return await Task.Run(()=> res.ToString());
        }
        /// <summary>
        /// 获取JS预加载数据
        /// </summary>
        /// <returns></returns>
        public async static Task<string> GetJsPreloading()
        {
            var tp = "<script src=\"{0}\" type=\"text/javascript\"></script>";
            var res = new StringBuilder();
            foreach (var s in _JsFileList)
            {
                res.Append(string.Format(tp, s));
            }
            return await Task.Run(() => res.ToString());
        }
    }
}
